/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import entity.Company;
import entity.Department;
import entity.Division;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import session.CompanyFacade;

/**
 *
 * @author owtee
 */
public class UserReqDivAndDep extends HttpServlet {

    @EJB
    private CompanyFacade companyFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                String comId = request.getParameter("comId");

                Company com = companyFacade.find(Integer.parseInt(comId));
                List<Division> diviLst = (List<Division>) com.getDivisionCollection();
                List<Department> depLst = (List<Department>) com.getDepartmentCollection();

                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
                //creating a new instance of a DOM to build a DOM tree.
                Document doc = docBuilder.newDocument();
                //new XmlServlet().createXmlTree(doc);

                Element msg = doc.createElement("Messenger");
                doc.appendChild(msg);
                // Create element division data
                Element tagDiviData = doc.createElement("DivisionData");
                tagDiviData.setAttribute("NumRecData", Integer.toString(diviLst.size()));
                msg.appendChild(tagDiviData);
                for (Division divi : diviLst) {
                    Element tagDivi = doc.createElement("Division");
                    tagDiviData.appendChild(tagDivi);
                    Element tagDiviId = doc.createElement("Id");
                    tagDivi.appendChild(tagDiviId);
                    Text txtDiviId = doc.createTextNode(divi.getDivId().toString());
                    tagDiviId.appendChild(txtDiviId);
                    Element tagDiviName = doc.createElement("Name");
                    tagDivi.appendChild(tagDiviName);
                    Text txtDiviName = doc.createTextNode(divi.getDivName());
                    tagDiviName.appendChild(txtDiviName);
                }

                // Create element department data
                Element tagDepData = doc.createElement("DepartmentData");
                tagDepData.setAttribute("NumRecData", Integer.toString(depLst.size()));
                msg.appendChild(tagDepData);
                for (Department dep : depLst) {
                    Element tagDep = doc.createElement("Department");
                    tagDepData.appendChild(tagDep);
                    Element tagDepId = doc.createElement("Id");
                    tagDep.appendChild(tagDepId);
                    Text txtDepId = doc.createTextNode(dep.getDepId().toString());
                    tagDepId.appendChild(txtDepId);
                    Element tagDepName = doc.createElement("Name");
                    tagDep.appendChild(tagDepName);
                    Text txtDepName = doc.createTextNode(dep.getDepName());
                    tagDepName.appendChild(txtDepName);
                }

                //TransformerFactory instance is used to create Transformer objects.
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();

                transformer.setOutputProperty(OutputKeys.INDENT, "yes");

                // create string from xml tree
                StringWriter sw = new StringWriter();
                StreamResult result = new StreamResult(sw);
                DOMSource source = new DOMSource(doc);
                transformer.transform(source, result);
                String xmlString = sw.toString();

                out.println(xmlString);

            } catch (Exception ex) {
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
